﻿using DevExpress.XtraEditors;
using DXApplicationTangche.UC.抖音出入库;
using DXApplicationTangche.UC.订单.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DXApplicationTangche.UC.订单.service.ViewSemiCheckExceptionDao;

namespace DXApplicationTangche.UC.报表
{
    public partial class Form成衣码对照 : DevExpress.XtraEditors.XtraForm
    {
        private List<DouyinOrderBarcodeMapFacadeDTO> douyinOrderBarcodeMapFacadeDTOs = new List<DouyinOrderBarcodeMapFacadeDTO>();
        private List<DouyinOrderBarcodeMapQueryFacadeDTO> douyindeleteBarcodeFromOrder = new List<DouyinOrderBarcodeMapQueryFacadeDTO>();
        private String orderNo;
        public Form成衣码对照()
        {
            InitializeComponent();
        }

        private void Form成衣码对照_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBox抖音订单号;
        }

        private void dataRefresh(String orderNo)
        {
            //  根据抖音订单号查询抖音订单明细
            this.douyinOrderBarcodeMapFacadeDTOs = 抖音客户Service.getDouyinOrderInfoByOrderNo(orderNo);
            if (this.douyinOrderBarcodeMapFacadeDTOs.Count() != 0)
            {
                this.orderNo = orderNo;
                // 绑定gridcontrol的data
                List<DouyinBarcodeQueryViewDTO> douyinBarcodeQueryViewDTOs = new DouyinBarcodeQueryViewModel(this.douyinOrderBarcodeMapFacadeDTOs).douyinBarcodeQueryViewDTOs;
                this.gridControl成衣码对照.DataSource = douyinBarcodeQueryViewDTOs;
                this.gridView成衣码对照.RefreshData();

                //  客户信息取第一行
                DouyinOrderBarcodeMapFacadeDTO douyinOrderBarcode = douyinOrderBarcodeMapFacadeDTOs[0];

                //创建一个DataTable,并为之添加数据(自定义DataTable)
                DataTable dt = new DataTable("cart");
                DataColumn dc1 = new DataColumn("customerName", Type.GetType("System.String"));
                DataColumn dc2 = new DataColumn("mobile", Type.GetType("System.String"));
                DataColumn dc3 = new DataColumn("addressProvince", Type.GetType("System.String"));
                DataColumn dc4 = new DataColumn("addressCity", Type.GetType("System.String"));
                DataColumn dc5 = new DataColumn("addressDistrict", Type.GetType("System.String"));
                DataColumn dc6 = new DataColumn("addressDetail", Type.GetType("System.String"));
                dt.Columns.Add(dc1);
                dt.Columns.Add(dc2);
                dt.Columns.Add(dc3);
                dt.Columns.Add(dc4);
                dt.Columns.Add(dc5);
                dt.Columns.Add(dc6);

                DataRow dr = dt.NewRow();
                dr["customerName"] = douyinOrderBarcode.customerName;
                dr["mobile"] = douyinOrderBarcode.mobile;
                dr["addressProvince"] = douyinOrderBarcode.addressProvince;
                dr["addressCity"] = douyinOrderBarcode.addressCity;
                dr["addressDistrict"] = douyinOrderBarcode.addressDistrict;
                dr["addressDetail"] = douyinOrderBarcode.addressDetail;
                dt.Rows.Add(dr);

                this.gridControl客户信息.DataSource = dt;
                this.gridView客户信息.RefreshData();
            }
            else
            {
                MessageBox.Show("订单号有误或者订单未同步");
            }



        }

        private void textBox抖音订单号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox抖音订单号.Text))
                {
                    MessageBox.Show("抖音订单号不能为空");
                    return;
                }
                // 根据抖音订单号查询抖音订单明细
                this.dataRefresh(this.textBox抖音订单号.Text.Trim());
                this.ActiveControl = this.textBox成衣码;
            }
        }

        private void textBox成衣码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox成衣码.Text))
                {
                    MessageBox.Show("成衣码不能为空");
                    return;
                }
                if (this.douyinOrderBarcodeMapFacadeDTOs.Count <= 0)
                {
                    MessageBox.Show("请先输入抖音订单号再扫描成衣码");
                    this.ActiveControl = this.textBox抖音订单号;
                    this.textBox成衣码.Clear();
                    return;
                }
                // 扫码完成订单标记
                DouyinOrderBarcodeMapQueryFacadeDTO douyinOrderBarcodeMapQueryFacadeDTO =
                    抖音客户Service.checkDouyinOrderBarcodeMapFacadeDTO(this.textBox成衣码.Text.Trim(), this.douyinOrderBarcodeMapFacadeDTOs);
                if (douyinOrderBarcodeMapQueryFacadeDTO.success)
                {
                    // 成功时刷新界面
                    this.douyinOrderBarcodeMapFacadeDTOs = douyinOrderBarcodeMapQueryFacadeDTO.douyinOrderBarcodeMapFacadeDTOS;
                    List<DouyinBarcodeQueryViewDTO> douyinBarcodeQueryViewDTOs = new DouyinBarcodeQueryViewModel(this.douyinOrderBarcodeMapFacadeDTOs).douyinBarcodeQueryViewDTOs;
                    this.gridControl成衣码对照.DataSource = douyinBarcodeQueryViewDTOs;
                    this.gridView成衣码对照.RefreshData();
                }
                else
                {
                    //  显示errorMessage
                    MessageBox.Show(douyinOrderBarcodeMapQueryFacadeDTO.errorMessage);
                }
                this.textBox成衣码.Clear();

                //  保存
                抖音客户Service.douyinBarcodeQueryViewDTOs(
                    this.orderNo,
                    new DouyinBarcodeQueryViewModel(this.douyinOrderBarcodeMapFacadeDTOs).douyinBarcodeQueryViewDTOs
                    );
            }
        }

        private void gridView成衣码对照_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            try
            {
                if (e.RowHandle >= 0)
                {
                    DouyinOrderBarcodeMapFacadeDTO checkDto = (DouyinOrderBarcodeMapFacadeDTO)gridView成衣码对照.GetRow(e.RowHandle);
                    if (checkDto.douyinOrderBarcodeMapDTOList.Count > 0)
                    {
                        e.Appearance.BackColor = Color.YellowGreen;
                        e.Appearance.ForeColor = Color.White;
                    }
                }
            }
            catch { }
        }

        private void toolStripMenuItem删除成衣码_Click(object sender, EventArgs e)
        {
            try 
            { 
                String barcode = this.gridView成衣码对照.GetRowCellValue(this.gridView成衣码对照.FocusedRowHandle, "barcode").ToString();
                foreach (DouyinOrderBarcodeMapQueryFacadeDTO item in this.douyindeleteBarcodeFromOrder)
                {
                    this.gridControl成衣码对照.DataSource = this.douyindeleteBarcodeFromOrder;
                    this.gridView成衣码对照.RefreshData();
                }
         
            }
            catch
            {
                return;
            }

        }
    }
}